package com.dou.sample.opengl_project;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLSurfaceView;
import android.opengl.GLUtils;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

/**
 * Created by Dou on 2018/1/15.
 */

public class TextureBitmap implements GLSurfaceView.Renderer{

    Bitmap bitmap;

    FloatBuffer textureFloatBuffer;

    float textureCoodinators[] = {
            0, 1,
            1, 1,
            0, 0,
            1, 0,
    };

    public TextureBitmap(Context context){
        bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.logo_s);


    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        int[] textures = new int[1];
        gl.glGenTextures(1, textures,0);

        ByteBuffer texturesByteBuffer = ByteBuffer.allocateDirect(textures.length * 4);
        texturesByteBuffer.order(ByteOrder.nativeOrder());
        textureFloatBuffer = texturesByteBuffer.asFloatBuffer();
        textureFloatBuffer.put(textureCoodinators);
        textureFloatBuffer.position(0);

        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);

        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);

        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {

    }

    @Override
    public void onDrawFrame(GL10 gl) {

    }
}
